package com.example.day02;

import java.util.Arrays;

public class ThreeArray {
    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] ints = deleteArrayelement(arr, 5);
        System.out.println(Arrays.toString(ints));
        int[] ints1 = deletesubscriptArray(arr, 0);
        System.out.println(Arrays.toString(ints1));

        int[] ints2 = dislocateArray(arr);
        System.out.println(Arrays.toString(ints2));
        int[] arr2 = {1, 2, 2, 2, 3, 3, 3, 6};
        int[] ints3 = doubleArray(arr2);
        System.out.println(Arrays.toString(ints3));
    }

    //从数组当中删除指定的元素
    public static int[] deleteArrayelement(int[] arr, int target) {
        //判断数组有效性
        if (arr == null || arr.length == 0) {
            return arr;
        }
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                count++;
            }
        }
        int[] newarr = new int[arr.length - count];
        int j = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                continue;
            }
            newarr[j++] = arr[i];
        }
        return newarr;

    }

    //根据数组下标删除指定元素
    public static int[] deletesubscriptArray(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return arr;
        }
        //考虑不周到
        if (target < 0 || target > arr.length) {
            return arr;
        }
        int[] temp = new int[arr.length - 1];
        int j = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i != target) {
                temp[j++] = arr[i];
            }
        }
        return temp;

    }

    //随机打乱一个数组
    public static int[] dislocateArray(int[] arr) {

        int[] temp = new int[arr.length];
        int k = 0;

        while (true) {

            int index = (int) (Math.random() * arr.length);
            temp[k++] = arr[index];

            arr = deletesubscriptArray(arr, index);
            if (arr.length == 0) {
                break;
            }
        }
        return temp;

    }

    //数组排重
    public static int[] doubleArray(int[] arr) {
        if (arr == null || arr.length == 0) {
            return arr;
        }
        int[] temp = new int[arr.length];

        int count = 0;

        do {
            temp[count++] = arr[0];
            arr = deleteArrayelement(arr, arr[0]);
            if (arr.length == 0) {
                break;
            }
        } while (true);


        return Arrays.copyOf(temp, count);
    }

    //排重方法二
    public static int[] doubleArray2(int []arr){
        if(arr==null || arr.length==0){
            return  arr;
        }
        for(int i=0;i<arr.length;i++){
            for (int j=i+i;j<arr.length;j++){
            }
        }
        return  null;
    }

}
